
var BPIC_HOST =  /^(http(s)?:\/\/[^\/]+)\//.exec(document.getElementById('blogapic_script').getAttribute('src'))[1];

function BPIC_load_script(url, id, callback) {
  var s=document.createElement('script');
  s.setAttribute('src',url);
  s.setAttribute('id', id);  
  s.onreadystatechange= function () {      
      //alert(this.readyState);
      if (this.readyState == 'complete' || this.readyState == 'loaded') {
      if (!this.bpic_onload_executed) { 
        this.bpic_onload_executed = true;
        callback(this.getAttribute('id'));
        }
      }
   };
  s.onload = function () {
    //alert('onload');
    if (!this.bpic_onload_executed) { 
      this.bpic_onload_executed = true;
      callback(this.getAttribute('id'));
      }
  };
  document.getElementsByTagName('body')[0].appendChild(s);
}

function loadGoogle() {
    var scriptElement = document.createElement("script");
    scriptElement.setAttribute("src", "http://www.google.com/jsapi?callback=loadGoogleModules");
    scriptElement.setAttribute("type", "text/javascript");
    document.getElementsByTagName("head").item(0).appendChild(scriptElement);
  }

function loadGoogleModules() {
	google.load("gdata", "1.x", {"callback" : bpic});
}

BPIC_load_script(BPIC_HOST+'/static/jquery-1.4.1.min.js', 'bpic_jquery', loadGoogle);



// Proceeds from here only if all scripts from BPIC_scripts_to_load are loaded 

function bpic () {  

$(document.createElement('link'))
  .attr('rel','stylesheet')
  .attr('type','text/css')
  .attr('id','bpic_bookmarklet_css')
  .attr('href',BPIC_HOST+'/static/bookmarklet.css')
  .appendTo('head');

/* UrlEncode & UrlDecode plugin */

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


var bpic_on = $(document.createElement('div'))
              .attr('id','bpic_on')
							.attr('style','display:none; position: absolute; background: red; color: white; padding:4px; z-index: 10001; left:10px; font-weight:bold;')
							.text('bpic is on')
							.appendTo('body');
							
function scroll_bpic_on(e) {
  var scrollY = this.scrollY || document.body.parentNode.scrollTop || document.body.scrollTop;
  bpic_on.css('top',(scrollY+10)+'px');
}
$(window).scroll(scroll_bpic_on);

function activate() {
  //elements = (elements == 'all'?$('*:not(.web_bpic_part, .web_bpic_part *, html, head, head *, body, script, style)'):elements);
  scroll_bpic_on(null);
	bpic_on.show();
	$('img').mouseover(bpic_mouseover)
          .mouseout(bpic_mouseout)
          .click(bpic_click);
}
activate();

function deactivate() {
  //elements = (elements == 'all'?$('*:not(.web_bpic_part, .web_bpic_part *, html, head, head *, body, script, style)'):elements);
  $('img').unbind('mouseover',bpic_mouseover)
          .unbind('mouseout',bpic_mouseout)
          .unbind('click',bpic_click);
	window.onbeforeunload = old_onbeforeunload;
}  



var old_onbeforeunload = window.onbeforeunload;
window.onbeforeunload = function (e) {
  var e = e || window.event;
  e.returnValue = "bpic is still running!";
  return "bpic is still running!";  
};


 
function bpic_mouseover(e) {    
    e.stopImmediatePropagation();
    $('.web_bpic_currentCSS').removeClass('web_bpic_currentCSS');	
		resize_img(this);    
		$(this).addClass('web_bpic_currentCSS');    
  }

function bpic_mouseout(e) {    
    e.stopImmediatePropagation();
    $(this).removeClass('web_bpic_currentCSS');
    restore_img(this);
  }
 

								
							
							
var pic = null;
var popup = $(document.createElement('div'))
              .attr('id','bpic_popup')              
              .attr('style','width:300px; border: 1px solid red; padding: 4px; background: white; position: absolute; z-index: 10000; displau: none;')
              .append('<label for="bpic_title">Title: </label><input style="width:250px" type="text" id="bpic_title"/><br />')
              .append('<label for="bpic_src">URL: </label><input style="width:250px; margin-top: 5px;" type="text" id="bpic_src"/><br />')
							
							.append($('<button style="float:right;margin-top:10px;margin-right:3px;" type="button">Submit!</button>').click(submit))
							.appendTo('body');
							
function bpic_click(e) {
	e.stopImmediatePropagation();
	pic = $(this);
	$('#bpic_src').val(pic.attr('src'));
	popup.css({	'top' : pic.position().top + 'px',
							'left' : (pic.position().left +pic.width() + 10) + 'px' })
				.show();
  return false;
  }

  
function resize_img(el){
  if (el.nodeName && 
      el.nodeName.toLowerCase() == 'img' && 
      !$(el).hasClass('web_bpic_similarCSS') &&
      !$(el).hasClass('web_bpic_selectedCSS') &&
      !$(el).hasClass('web_bpic_currentCSS')) {
    $(el).width($(el).width() - 2).height($(el).height() - 2);
    el.web_bpic_resize_ok = false;
  }
}
function restore_img(el) {
  if (el.nodeName && 
      el.nodeName.toLowerCase() == 'img' &&
      !$(el).hasClass('web_bpic_similarCSS') &&
      !$(el).hasClass('web_bpic_selectedCSS') &&
      !$(el).hasClass('web_bpic_currentCSS')) {
    $(el).width($(el).width() + 2).height($(el).height() + 2);
    }
}



function submit() {
	alert('Blogger!');
}



 /* 
 
var user_info = $(document.createElement('div'))
                  .attr('id','bpic_user_info')                  
                  .html('<div id="bpic_user_greeting" class="web_bpic_heading" style="width:70%; float:left;"></div>')                  
                  .appendTo(popup);

                  

  
var bpic_page_info = $(document.createElement('div'))
                  .attr('id','bpic_page_info')
                  .html('<div><span class="web_bpic_heading" style="width:60% !important;">Loading page info </span><img style="margin-left:6px !important;" src="'+BPIC_HOST+'/static/ajax-loader.gif" alt="loader" /></div>')
                  .appendTo(popup);                  
                 
var page_bpic_objects = new Array();
*/
/*
$("#bpic_popup").dialog({
  dialogClass: 'web_bpic_popup web_bpic_part',
  width : 360,
  open : function () {
    activate();
  },
  close : function () {
    $('.web_bpic_popup, #bpic_popup, #bpic_css_file, #blogapic_script, #bpic_jquery, #bpic_theme, #bpic_bookmarklet_css').remove();
    for (script in BPIC_scripts_to_load) {
      $('#'+script).remove();
    }
    deactivate();      
    window.onbeforeunload = old_onbeforeunload;
  } ,
  title : 'bpic',
  dragStart: function(event, ui) { popup_dragging = true; },
  dragStop: function(event, ui) { popup_dragging = false; },
  resizeStart: function(event, ui) { popup_dragging = true; },
  resizeStop: function(event, ui) { popup_dragging = false; }    
}).mouseenter(function (e) { $('.web_bpic_currentCSS').removeClass('web_bpic_currentCSS'); });



$('#bpic_popup')
  .dialog('option', 'position' , [$(window).width() - $('.web_bpic_popup').width() - 15, 10])
  .dialog('option','show', 'slide')
  .dialog('open');  
*/
  
/*  
  
  
function logMeIn() {
  scope = "http://www.blogger.com/feeds";
  var token = google.accounts.user.login(scope);
}

function setupMyService() {
  var myService = new google.gdata.blogger.BloggerService('petko-bpic-1');
  logMeIn();
  return myService;
}

if (google.accounts.user.checkLogin("http://www.blogger.com/feeds")) {
 // Retrieve a list of blogs
   

  // Create the blogger service object
  var bloggerService = new google.gdata.blogger.BloggerService('petko-bpic-1.0');
  // The default "metafeed" feed is used to retrieve a list of blogs for a particular logged-in user
  var feedUri = 'http://www.blogger.com/feeds/default/blogs';
  // The callback method invoked when getBlogFeed() returns feed data
  var handleBlogFeed = function(blogFeedRoot) {
    var blogEntries = blogFeedRoot.feed.getEntries();    
    // Has the user created any blogs?
    if(!blogEntries.length) {
      $('#bpic_blogs').text('There are no blogs for this account.');
      return;
    }
    
    $('#bpic_blogs').append('<span>Blog: </span><select id="bpic_blog"></select>');
    for (var i = 0, blogEntry; blogEntry = blogEntries[i]; i++) {
      $('#bpic_blog').append('<option value='+blogEntry.getHtmlLink().getHref()+
                             '>'+blogEntry.getTitle().getText()+'</option>');
      //var blogTitle = blogEntry.getTitle().getText();
      //var blogURL = blogEntry.getHtmlLink().getHref();
      //var blogUpdated = blogEntry.getUpdated().getValue().getDate();
      
      
    }
  };

  // Error handler called when getBlogFeed() produces an error
  var handleError = function(error) {
    alert(error);
  };

  // Submit the request using the blogger service object
  bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);
} else {
  $('#bpic_blog').append('Login to blogger!');
}
                  
*/
  
  
};
